package www.shuang.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.controller.BaseController;
import www.shuang.investor.pojo.AInvestor;
import www.shuang.investor.service.IAInvestorService;
import www.shuang.orders.pojo.AOrders;
import www.shuang.orders.service.AOrdersService;
import www.shuang.users.pojo.AUsers;
import www.shuang.users.pojo.AUsersCard;
import www.shuang.users.service.IAAdminsService;

@Controller
@RequestMapping("/head/orders/")
public class OrdersHeadController extends BaseController
{
	@Resource
	private IAAdminsService  adminsService;
	@Resource
	private IAInvestorService investorService;
	@Resource
	private AOrdersService ordersService;
	
	@RequestMapping("/ordersList.htm")
	public String ordersList(){
		return "head/orders/ordersList";
	}
	/*
	 * 订单模块的controller
	 * 查询当前用户下的所有卡信息
	 * 出资方传过来
	 */
	@RequestMapping("/OrdersInsert.htm")
	 public String OrdersInsert(@RequestParam String iid,HttpServletRequest request){
		 
		HttpSession session=request.getSession();
			AUsers users=(AUsers) session.getAttribute("users");
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.clear();
			condMap.put("usersId", users.getId());
			//查询所有银行卡信息
			List<AUsersCard> usersCardList = this.adminsService.findCondListUsersCardService(null, condMap);
			 	request.setAttribute("usersCardList", usersCardList);
			 	condMap.clear();
			 	//查询出资方
			 	condMap.put("id",iid);
			 	AInvestor investor = this.investorService.findOneAInvestorService(condMap);
			 	request.setAttribute("investor", investor);
			 return "/head/orders/OrdersInsert";
	 }	
		//订单提交页面
	@RequestMapping("/OrdersInsertSubmit.htm")
	public String OrdersInsertSubmit(AOrders orders,@RequestParam String iid,HttpServletRequest request){
		/*接收参数，处理参数，new对象，赋值，对象属性驱动*/
		HttpSession session = request.getSession();
		AUsers users=(AUsers) session.getAttribute("users");
		Map<String,Object> condMap=new HashMap<String,Object>();
		//查询单个银行卡信息
		condMap.clear();
		condMap.put("id", orders.getCardId());
		AUsersCard usersCard = this.adminsService.findOneUsersCardService(condMap);
		orders.setCardno(usersCard.getCardno());
		orders.setRegionId(usersCard.getRegionId());
		orders.setAddress(usersCard.getAddress());
		//查询出资方
		condMap.clear();
	 	condMap.put("id",iid);
	 	AInvestor investor = this.investorService.findOneAInvestorService(condMap);
		orders.setLoanRate(investor.getLoanRate());
		orders.setOverdueRate(investor.getOverdueRate());
		
		orders.setRepaymentDate(new Date());
		//
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		
		JSONObject resultJSON = this.ordersService.insertOneAOrdersService(orders);
			if("0".equalsIgnoreCase(resultJSON.getString("code"))){
				return "redirect:/head/orders/ordersList.htm";
			}else{
				this.info=resultJSON.getString("info");
			}
		return this.OrdersInsert(iid, request);
	}
}
